package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Price;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.Seat;
import com.ruoyi.system.service.IPriceService;
import com.ruoyi.system.service.IRoomService;
import com.ruoyi.system.service.ISeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * 座位Controller
 *
 * @author ...
 * @date 2022-03-17
 */
@RestController
@RequestMapping("/system/seat")
public class SeatController extends BaseController {
    @Autowired
    private ISeatService seatService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IPriceService priceService;

    /**
     * 查询座位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Seat seat) {
        startPage();
        List<Seat> list = seatService.selectSeatList(seat);
        List<Seat> list1=new LinkedList<>();
        for (Seat seat1 : list) {
            Price price = priceService.selectPriceById(Long.parseLong(seat1.getTypeId()));
            seat1.setPrice(price.getPrice());
            list1.add(seat1);
        }
        return getDataTable(list1);
    }

    /**
     * 获取座位详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(seatService.selectSeatById(id));
    }

    /**
     * 新增座位
     */
    @PostMapping
    public AjaxResult add(@RequestBody Seat seat) {
        Seat seat1 = new Seat();
        seat1.setRoomId(seat.getRoomId());
        seat1.setNum(seat.getNum());
        List<Seat> seats = seatService.selectSeatList(seat1);
        Assert.isTrue(seats.size()==0,"座位编号重复");
        Room room = roomService.selectRoomById(Integer.parseInt(seat.getRoomId()));
        Price price = priceService.selectPriceById(Long.parseLong(seat.getTypeId()));
        seat.setRoomName(room.getName());
        seat.setType(price.getType());
        seat.setCreateUser(SecurityUtils.getUserId().intValue());
        seat.setCreateTime(DateUtils.getNowDate());
        return toAjax(seatService.insertSeat(seat));
    }

    /**
     * 修改座位
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Seat seat) {
        Room room = roomService.selectRoomById(Integer.parseInt(seat.getRoomId()));
        // Price price = priceService.selectPriceById(Long.parseLong(seat.getType()));
        seat.setRoomName(room.getName());
        // seat.setType(price.getType());
        seat.setUpdateUser(SecurityUtils.getUserId().intValue());
        seat.setUpdateTime(DateUtils.getNowDate());
        return toAjax(seatService.updateSeat(seat));
    }

    /**
     * 删除座位
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(seatService.deleteSeatByIds(ids));
    }
}
